package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.itheima.core.controller.AbstractCoreController;

import java.util.Map;

/**
 * @description <p>自媒体图文内容信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmNewsController",tags = "自媒体图文内容信息")
@RestController
@RequestMapping("/news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }

    /**
     * 内容分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResultVo<WmNews> findPage(@RequestBody WmNewsPageReqDto dto){
      PageResultVo<WmNews> wmNews =  wmNewsService.findPage(dto);
        return wmNews;
    }

    /**
     * 发布文章
     * @param draft
     * @param wmNewsDtoSave
     * @return
     */
    @PostMapping("/submit")
    public ResultVo submit(boolean draft, @RequestBody WmNewsDtoSave wmNewsDtoSave){
         wmNewsService.submit(draft,wmNewsDtoSave);
         return ResultVo.ok();

    }

    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    @GetMapping("/one/{newsId}")
    public ResultVo<WmNews> findWmNewsById(@PathVariable(value = "newsId")Long id){
        WmNews wmNews = wmNewsService.findWmNewsById(id);
        return ResultVo.ok(wmNews);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @GetMapping("/del_news/{id}")
    public ResultVo deleteById(@PathVariable(value = "id")Long id){
        wmNewsService.deleteById(id);
        return ResultVo.ok("删除成功");
    }

    @PostMapping("/down_or_up")
    public ResultVo downOrUp(@RequestBody Map<String,String> map){
        wmNewsService.downOrUp(map);
        return ResultVo.ok("操作成功");
    }
}

